function lobby.load()
   local file = io.open(minetest.get_worldpath() .. '/levels', 'r')
   if file then
      lobby.savedata = minetest.deserialize(file:read())
      file:close()
   else
      lobby.savedata = {}
      lobby.savedata.IDs = {}
      lobby.savedata.data = {}
   end
end

function lobby.save()
   local file = io.open(minetest.get_worldpath() .. '/levels', 'w')
   file:write(minetest.serialize(lobby.savedata))
   file:close()
end

function lobby.corpse_entry(pos, map_id)
   local corpse_pos = lobby.corpses[map_id]
   table.insert(corpse_pos, pos)
   lobby.corpses[map_id] = corpse_pos
end

function lobby.corpse_removal(map_id)
   local corpse_pos = lobby.corpses[map_id]
   for key, value in pairs(corpse_pos) do
      minetest.set_node(value, {name='air'})
   end
end

function lobby.players_on_level(map_id)
   local survivors = ''
   for _, player in pairs(minetest.get_connected_players()) do
      local rname  = player:get_player_name()
      if lobby.game[rname] == map_id then
         survivors = survivors..rname..', '
      end
   end
   return survivors
end

function lobby.update_maps(map_name)
   local count = lobby.map[map_name] - 1
   if count <= 0 then
      lobby.map[map_name] = 0
   else
      lobby.map[map_name] = count
   end
end

function lobby.vote(map_id, force)
   local needed_votes = lobby.map[map_id] - 1
   local actual_votes = lobby.votes[map_id]
   if actual_votes >= needed_votes or force then
      lobby.vote_timer[map_id] = 0
      --Reset vote status and kick player with the highest score.
      local high_score = 0
      local kick = ''
      local traitor = lobby.traitors[map_id]
      for _, player in pairs(minetest.get_connected_players()) do
         local rname  = player:get_player_name()
         if lobby.game[rname] == map_id then
            local score = lobby.suspect[rname]
            if score > high_score then
               high_score = score
               kick = rname
            end
            lobby.suspect[rname] = 0
            player:set_physics_override({speed=1})
            if kick == traitor then
               lobby.team_win(map_id)
            elseif needed_votes == 2 and kick ~= traitor then
               lobby.traitor_win(traitor, map_id)
            end
         end
      end
      if kick ~= '' then
         lobby.message_to_level(map_id, 'The votes are in, '..kick..' will be kicked.')
         player = minetest.get_player_by_name(kick)
         player:setpos({x=0, y=0, z=0})
         local player_inv = player:get_inventory()
         player_inv:set_list('main', {})
         lobby.game[kick] = 'lobby'
         lobby.update_maps(map_id)
      else
         lobby.message_to_level(map_id, 'Nobody was kicked this round.')
      end
   end
end

function lobby.team_win(map_id)
   lobby.corpse_removal(map_id)
   for _, player in pairs(minetest.get_connected_players()) do
      local rname  = player:get_player_name()
      if lobby.game[rname] == map_id then
         minetest.chat_send_player(rname, 'The traitor was defeated.')
         player:setpos({x=0, y=0, z=0})
         player:set_physics_override({speed=1})
         local player_inv = player:get_inventory()
         player_inv:set_list('main', {})
         lobby.game[rname] = 'lobby'
      end
   end
   lobby.map[map_id] = 0
   minetest.chat_send_all('The traitor was defeated on this round in the '..map_id)
end

function lobby.traitor_win(traitor, map_id)
   local player = minetest.get_player_by_name(traitor)
   lobby.corpse_removal(map_id)
   minetest.chat_send_all('The traitor won this round on '..map_id..'.')
   minetest.chat_send_player(traitor, 'CONGRATULATIONS!!! You killed all the crew!')
   lobby.game[traitor] = 'lobby'
   player:setpos({x=0, y=0, z=0})
   player:set_physics_override({speed=1})
   local player_inv = player:get_inventory()
   player_inv:set_list('main', {})
   lobby.map[map_id] = 0
end
